<?php

namespace App\Http\Controllers\Admin\Superadmin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AuthorController extends Controller
{
    /*
		作者申请的操作
    */
	public function getAdd(request $request)
	{
		//获取所有status 为1的用户
		$name = DB::table('hs_member')->where('state','1')->get();
		//$time = date('Y-m-d H:i:s',time());
		//解析模板
		return view ('admin.superadmin.author.add',['name'=>$name]);
	}

	/*
		同意成为作者
	*/
	public function getAgree($id){
		
		$data['make'] = 1;
		$res = DB::table('hs_member')->where('id',$id)->update($data);
		//判断
		if($res){
			$sta['state'] = 0;			
			$res = DB::table('hs_member')->where('id',$id)->update($sta);
			return view('admin/superadmin/author/index');

		}else{
			return back()->with('error','审核失败');
		}

	}	

	/*
		拒绝成为作者
	*/

	public function getRefused($id)
	{
		$data['state'] = 0;
		$res = DB::table('hs_member')->where('id',$id)->update($data);
		return redirect('/admin/superadmin/author/add');

	}


	/*
		作者列表	
	*/	
	public function getIndex(request $request)
	{
		//查询所有的作者
		$author = DB::table('hs_member')->where('make',1)->get();
		$fauthor = [];
		foreach($author as $k=>$v){
			if(!DB::table('hs_relation')->where('aid',$v->id)->first()){
				$fauthor[] = $author[$k];
			}
		}
		// dd($fauthor);
			// ->join('hs_relation','hs_relation.aid','=','hs_member.id')
	
			


		/*$user = DB::table('hs_member')
		->join('hs_editor','hs_member.id','=','hs_editor.au_id' )
		->select('hs_editor.*','hs_member.name')
		->where('hs_member.make','=', '1')
		->get();
		dd($user);*/

		return view ('admin.superadmin.author.index',['fauthor'=>$fauthor]);
	}	

	/*
		关于作者删除的操作
	*/
	public function getDelete($id)
	{
			
		//执行删除
		$res = DB::table('hs_member')->where('id',$id)->delete();
	if($res){
		return redirect('/admin/superadmin/author/index')->with('success',删除成功);
	}else{
		return back()->with('error',删除失败);
	}

	}

	/*
		关于作者分配的操作
	*/

	public function getEdit($id)
	{
		$user = DB::table('hs_editor')->get();
		// dd($user);
		foreach ($user as $k => $v) {
			// var_dump($v->id);
			$num = DB::table('hs_relation')->where('eid','=',$v->id)->count();
			// var_dump($num);
			$user[$k]->num = $num;
		}

		$author = DB::table('hs_member')->where('id',$id)->first();
		// dd($user);
		return view('admin.superadmin.author.edit',['user'=>$user,'author'=>$author]);
	}	

	/*
		执行数据的插入
	*/
	public function postUpdate(request $request)
	{
				
	//获取id
		$data = $request->except('_token');
	
		//执行分配插入
		$res = DB::table('hs_relation')->insert($data);
		//判断是否添加成功
		if($res){
			return redirect('/admin/superadmin/user/index')->with('success','用户分配成功');
		}else{
			return back()->with('error','用户分配失败');
		}		 

	}	



}	


